package com.study.forumsystemproject.service.impl;

import com.study.forumsystemproject.common.Result;
import com.study.forumsystemproject.common.ResultCode;
import com.study.forumsystemproject.dao.BoardMapper;
import com.study.forumsystemproject.exception.ApplicationException;
import com.study.forumsystemproject.model.Board;
import com.study.forumsystemproject.service.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * program: forum-system-project
 * <p>
 * description: 版块接口实现
 * <p>
 * author: xiaoxie
 * <p>
 * create: 2024-08-16 23:05
 **/
@Service
@Slf4j
public class BoardImpl implements IBoardService {

    @Autowired
    private BoardMapper boardMapper;
    /**
     * Description: 获取版块列表
     * Param: 版块展示的数量
     * return: 版块列表
     * Author: xiaoxie
     * Date: 11:18 2024/8/17
    */
    @Override
    public List<Board> getBoards(Integer num) {
        if (num == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        return boardMapper.selectAllBoardByNum(num);
    }
    /**
     * Description: 通过id添加对应版块的文章数量
     * Param:board id 文章count
     * return: void
     * Author: xiaoxie
     * Date: 11:03 2024/8/17
     */
    @Override
    public void addOneArticleById(Long id, Integer count) {
        //参数校验
        if(id == null || count == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE+ "board id : " +id);
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //创建更新对象.
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(count+1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //结果校验
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_CREATE));
        }
    }
    /**
     * Description: 通过id删除对应版块的文章数量
     * Param:board id 文章count
     * return: void
     * Author: xiaoxie
     * Date: 11:03 2024/8/17
     */

    @Override
    public void subOneArticleById(Long id) {
        //参数校验
        if(id == null ) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE+ "board id : " +id);
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null || board.getDeleteFlag() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        if(board.getState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_BOARD_BANNED));
        }
        //创建更新对象.
        Board updateBoard = new Board();
        updateBoard.setId(id);
        updateBoard.setArticleCount(board.getArticleCount()-1);
        if(board.getArticleCount() -1 < 0) {
            updateBoard.setArticleCount(0);
        }
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //结果校验
        if(row != 1) {
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_CREATE));
        }
    }

    @Override
    public Board getBoardById(Long id) {
        if(id == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE+ "board id : " +id);
            throw new ApplicationException(Result.fail(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS+" board id : " +id);
            throw new ApplicationException(Result.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        if(board.getDeleteFlag() != 0 ) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS+" board id : " +id);
            throw new ApplicationException(Result.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return board;
    }
}
